package com.flyduck.vueshop.user.remote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyduck.vueshop.common.core.utils.BeanUtils;
import com.flyduck.vueshop.user.remote.service.RemoteUserService;
import com.flyduck.vueshop.user.vo.RemoteUserRespVO;
import com.flyduck.vueshop.user.vo.RemoteUserSaveReqVO;
import com.flyduck.vueshop.user.vo.RemoteUserUpdateReqVO;
import com.flyduck.vueshop.user.entity.AppUser;
import com.flyduck.vueshop.user.mapper.AppUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * AppUserApiServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-07-10
 */
@Service
public class RemoteUserServiceImpl implements RemoteUserService {

    @Resource
    private AppUserMapper appUserMapper;

    @Override
    public RemoteUserRespVO getUserByUsername(String username) {
        AppUser appUser = appUserMapper.selectOne(
                new LambdaQueryWrapper<AppUser>()
                        .eq(AppUser::getUsername, username)
        );
        RemoteUserRespVO remoteUserRespVO = BeanUtils.toBean(appUser, RemoteUserRespVO.class);
        return remoteUserRespVO;
    }

    @Override
    public void saveUser(RemoteUserSaveReqVO remoteUserSaveReqVO) {
        AppUser appUser = BeanUtils.toBean(remoteUserSaveReqVO, AppUser.class);
        appUserMapper.insert(appUser);
    }

    @Override
    public void updateUser(RemoteUserUpdateReqVO remoteUserUpdateReqVO) {
        AppUser appUser = BeanUtils.toBean(remoteUserUpdateReqVO, AppUser.class);
        appUserMapper.updateById(appUser);
    }

    @Override
    public RemoteUserRespVO getUserById(Long id) {
        AppUser appUser = appUserMapper.selectById(id);
        RemoteUserRespVO remoteUserRespVO = BeanUtils.toBean(appUser, RemoteUserRespVO.class);
        return remoteUserRespVO;
    }
}
